package com.lanou.lenanapp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.lanou.lenanapp.R;
import com.lanou.lenanapp.adapter.SpecialAdapter;
import com.lanou.lenanapp.beans.Special;
import com.lanou.lenanapp.volley.VolleySingleton;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 专辑
 */
public class SpecialActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private ImageView back_special;
    private ListView specialListView;
    private SpecialAdapter specialAdapter;
    VolleySingleton volleySingleton = VolleySingleton.getInstance();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_special);

        init();
        initData();

    }

    private void init() {

        back_special = (ImageView) findViewById(R.id.back_special);
        back_special.setOnClickListener(this);
        specialListView = (ListView) findViewById(R.id.specialListView);
        specialListView.setOnItemClickListener(this);
        specialAdapter = new SpecialAdapter();
        specialListView.setAdapter(specialAdapter);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back_special:
                finish();
                break;
        }
    }



    private void initData() {

        String url = "http://120.55.151.67/weibofun/albums/album_list.php?apiver=10701&category=weibo_girls&page=0" +
                "&page_size=30&max_timestamp=-1&platform=iphone&appver=1.9&buildver=1.9.4" +
                "&udid=0FD678F7-07AA-4EBF-BB39-EB79EAB40753&sysver=7.0.4";
        JsonObjectRequest request = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                if(response != null && response.has("items")){
                    try {
                        JSONArray item = response.getJSONArray("items");
                        List<Special> specials = new ArrayList<Special>();
                        Gson gson = new Gson();
                        for (int i = 0; i < item.length(); i ++){
                            String str = item.getString(i);
                            Special special = gson.fromJson(str, Special.class);
                            specials.add(special);
                        }
                        specialAdapter.addData(specials);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        volleySingleton.addRequest(request);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Special special = (Special) parent.getAdapter().getItem(position);
        String aicon = special.getAicon();
        String title = special.getAname();
        String likes = special.getLikes();
        String aid = special.getAid();
        Intent intent = new Intent(this, SpecialGutActivity.class);
        intent.putExtra("aicon", aicon);
        intent.putExtra("title", title);
        intent.putExtra("likes", likes);
        intent.putExtra("aid", aid);
        startActivity(intent);
    }
}
